	{% load dict_lookup %}
	<div class="sectionHeading">Setup Network</div>
	{% include "includes/actionmessages.html" %}
	{% include "includes/errors.html" %}
	<div class="requiredFields">
		* Indicates mandatory fields.
	</div>
	<form method="post" id="saveNetworkForm" name="saveNetworkForm" action="saveNetwork" class="cssForm" onsubmit="return postForm(this,'content-right')">
		<div class="formDiv">
			<label class="formLabel">Host Name <span class="required">*</span> : </label>
			<input type="text" name="hostname" value="{{ form.hostname }}">
		</div> 
		<div class="formDiv">
			<label class="formLabel">Domain Name <span class="required">*</span> : </label>
			<input type="text" name="domainname" value="{{ form.domainname }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Interface <span class="required">*</span> : </label>
			<select id="interface" name="interface">
			{% for iface in form.interfaces %}
				<option value="{{ iface}}" {% if form.default_phyiface == iface %} selected="selected" {% endif %}> {{ iface }} </option>
			{% endfor %}
			</select>	
		</div>
		<div class="formDiv">
			<label class="formLabel">Obtain an IP Address from DHCP : </label> 
			<input type="checkbox" id="dhcp" name="dhcp" {% if form.dhcp %} checked="checked" {% endif %}>
		</div>
		<div class="formDiv">
			<label class="formLabel">IP Address <span class="required">*</span> : </label>
			<input type="text" id="ipaddr" name="ipaddr" value="{{ form.ipaddr }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Network Mask <span class="required">*</span> : </label>
			<select id="subnet" name="subnet">
			{% for mask in subnets %}
				<option value="{{ mask}}" {% if form.subnet == mask %} selected="selected" {% endif %}> {{ mask }} </option>
			{% endfor %}
			</select>	
		</div>
		<div class="formDiv">
			<label class="formLabel">Default Gateway <span class="required">*</span> : </label>
			<input type="text" id="gateway" name="gateway" value="{{ form.gateway }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Primary DNS Server Address <span class="required">*</span> : </label>
			<input type="text" id="dns1" name="dns1" value="{{ form.dns1 }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Secondary DNS Server Address <span class="required">*</span> : </label>
			<input type="text" id="dns2" name="dns2" value="{{ form.dns2 }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Enable VLAN : </label> 
			<input type="checkbox" id="nativeVlan" name="nativeVlan" {% if form.vlanid != "" or form.enableNativeVlan == "on" %} checked="checked" {% endif %}>
		</div>
		<div class="formDiv">
			<label class="formLabel">VLAN ID : </label>
			<input type="text" id="vlanid" name="vlanid" value="{{ form.vlanid }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">&nbsp;</label>
			<div class="formBtnFormat">
				<input class="btn" type="submit" value="Save"> &nbsp;&nbsp; <input class="btn" type="button" value="Reset" onclick="javascript:toggleAll();">
			</div>
		</div>
		
		<div><label>&nbsp;&nbsp;&nbsp;</label></div>
		
		{% if form.phyInterfacesInfo %}
		<div>
		<table width="95%" align="center" border="0" cellspacing="0" cellpadding="4">
		<tr>
			<th class="heading">Interface </th>
			<th class="heading">IP Address </th>
			<th class="heading">Subnet Mask </th>
			<th class="heading" width="10%">Action</th>
		</tr>
		{% for iface in form.phyInterfacesInfo %}
		<tr>
			<td class="element"><input type="hidden" id="interface{{forloop.counter}}" name="interface{{forloop.counter}}" value="{{ iface.0 }}">{{ iface.0 }}</td>
			<td class="element"><input type="hidden" id="ipaddress{{forloop.counter}}" name="ipaddress{{forloop.counter}}" value="{{ iface.1 }}">{{ iface.1 }}</td>
			<td class="element"><input type="hidden" id="netmask{{forloop.counter}}" name="netmask{{forloop.counter}}" value="{{ iface.2 }}">{{ iface.2 }}</td>
			<td width="10%">
				{% if form.vlanid == '' %}
					{% if iface.0 != form.default_phyiface %}
						<label><img class="action" align="left" title="edit interface" onclick="javascript:showLoading('Loading..');get('editPhyIface?iface={{ iface.0 }}\&ip={{ iface.1 }}\&nm={{ iface.2 }}','content-right');" src="/img/sroute_edit.png"/></label>
						<label><img class="action" align="right" title="delete interface" onclick="javascript:confirmPhyIfaceDelete('{{ iface.0 }}');" src="/img/sroute_delete.png"/></label>
					{% endif %}
				{% endif %}
			</td>
		</tr>
		{% endfor %}
		</table>
		</div>
		{% endif %}
	</form>
	

	<div><label>&nbsp;&nbsp;&nbsp;</label></div>
	<div><label>&nbsp;&nbsp;&nbsp;</label></div>
	
	<div class="sectionHeading">VLAN Configuration</div>
	<div class="requiredFields">
		* Indicates mandatory fields.
	</div>
	{% if vlanErrors %}
	<div style="padding: 5px 0px;">
		<ul>
		    {% for error in vlanErrors %}
		    	<li class="errorMsg">{{ error }}</li>
		    {% endfor %}
		</ul>
	</div>
	{% endif %}
	<li class="info">{{vlanStatus}}</li>
	<form method="post" id="saveVlanForm" name="saveVlanForm" action="saveVlanSettings" class="cssForm" onsubmit="return postForm(this,'content-right')">
		<div class="formDiv">
			<label class="formLabel">VLAN ID <span class="required">*</span> : </label>
			<input type="text" id="vlanID" name="vlanID" value="{{ vlanForm.vlanID }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">IP Address <span class="required">*</span> : </label>
			<input type="text" id="vlanIP" name="vlanIP" value="{{ vlanForm.vlanIP }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Subnet <span class="required">*</span> : </label>
			<select id="vlanSubnet" name="vlanSubnet">
			{% for mask in subnets %}
				<option value="{{ mask }}" {% if vlanForm.vlanSubnet == mask %} selected="selected" {% endif %}> {{ mask }} </option>
			{% endfor %}
			</select>
		</div>
		<div class="formDiv">
			<label class="formLabel">Interface <span class="required">*</span> : </label>
			<select id="vlanInterface" name="vlanInterface">
			<option>select</option>
			{% for iface in form.interfaces %}
				<option value="{{ iface }}" {% if vlanForm.vlanInterface == iface %} selected="selected" {% endif %}> {{ iface }} </option>
			{% endfor %}
			</select>
		</div>
		<div class="formDiv">
			<label class="formLabel">&nbsp;</label>
			<div class="formBtnFormat">
				<input class="btn" type="submit" value="Save"> &nbsp;&nbsp; 
				<input class="btn" type="button" value="Reset" onclick="javascript:toggleVlanForm();">
			</div>
		</div>
		
		<div><label>&nbsp;&nbsp;&nbsp;</label></div>
		
		{% if form.vlans %}
		<div>
		<table width="95%" align="center" border="0" cellspacing="0" cellpadding="4">
		<tr>
			<th class="heading">VLAN ID </th>
			<th class="heading">IP Address </th>
			<th class="heading">Subnet Mask </th>
			<th class="heading">Interface </th>
			<th class="heading" width="10%">Action</th>
		</tr>
		{% for vlan in form.vlans %}
		<tr>
			<!-- <td width="20" align="left"><input id="vlanCBox" name="vlanCBox" type="checkbox" value="checkbox" /></td> -->
			<td class="element"><input type="hidden" id="vlanID{{forloop.counter}}" name="vlanID{{forloop.counter}}" value="{{ vlan.0 }}">{{ vlan.0 }}</td>
			<td class="element"><input type="hidden" id="vlanIP{{forloop.counter}}" name="vlanIP{{forloop.counter}}" value="{{ vlan.1 }}">{{ vlan.1 }}</td>
			<td class="element"><input type="hidden" id="vlanSubnet{{forloop.counter}}" name="vlanSubnet{{forloop.counter}}" value="{{ vlan.2 }}">{{ vlan.2 }}</td>
			<td class="element"><input type="hidden" id="vlanInterface{{forloop.counter}}" name="vlanInterface{{forloop.counter}}" value="{{ vlan.3 }}">{{ vlan.3 }}</td>
			<td width="10%">
				{% if form.vlanid != vlan.0 %}
					<label><img class="action" align="left" title="edit vlan" onclick="javascript:showLoading('Loading..');get('editVlan?id={{ vlan.0 }}\&iface={{ vlan.3 }}\&vlan={{ vlan.4 }}','content-right');" src="/img/sroute_edit.png"/></label>
					<label><img class="action" align="right" title="delete vlan" onclick="javascript:confirmVlanDelete('{{ vlan.4 }}');" src="/img/sroute_delete.png"/></label>
				{% endif %}
			</td>
		</tr>
		{% endfor %}
		</table>
		</div>
		{% endif %}
	</form>
	
	<div><label>&nbsp;&nbsp;&nbsp;</label></div>
	<div><label>&nbsp;&nbsp;&nbsp;</label></div>
	
	<div class="sectionHeading">Static Routes Configuration</div>
	<div class="requiredFields">
		* Indicates mandatory fields.
	</div>
	{% if srouteErrors %}
	<div style="padding: 5px 0px;">
		<ul>
		    {% for error in srouteErrors %}
		    	<li class="errorMsg">{{ error }}</li>
		    {% endfor %}
		</ul>
	</div>
	{% endif %}
	<li class="info">{{srouteStatus}}</li>
	<form method="post" id="saveRouteForm" name="saveRouteForm" action="saveStaticRoute" class="cssForm" onsubmit="return postForm(this,'content-right')">
		<div class="formDiv">
			<label class="formLabel">Network <span class="required">*</span> : </label>
			<input type="text" id="srouteIP" name="srouteIP" value="{{ srouteForm.srouteIP }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Subnet <span class="required">*</span> : </label>
			<select id="srouteSubnet" name="srouteSubnet">
			{% for mask in subnets %}
				<option value="{{ mask }}" {% if srouteForm.srouteSubnet == mask %} selected="selected" {% endif %}> {{ mask }} </option>
			{% endfor %}
			</select>
		</div>
		<div class="formDiv">
			<label class="formLabel">Gateway <span class="required">*</span> : </label>
			<input type="text" id="srouteGateway" name="srouteGateway" value="{{ srouteForm.srouteGateway }}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Metric <span class="required">*</span> : </label>
			<input type="text" id="srouteMetric" name="srouteMetric" value="{% if srouteForm.srouteMetric %}{{ srouteForm.srouteMetric }}{% else %}0{% endif %}">
		</div>
		<div class="formDiv">
			<label class="formLabel">Interface(Optional) : </label>
			<select id="srouteInterface" name="srouteInterface">
			<option></option>
			{% for iface in form.allinterfaces %}
				<option value="{{ iface }}" {% if srouteForm.srouteInterface == iface %} selected="selected" {% endif %}> {{ iface }} </option>
			{% endfor %}
			</select>
		</div>
		<div class="formDiv">
			<label class="formLabel">&nbsp;</label>
			<div class="formBtnFormat">
				<input class="btn" type="submit" value="Add Route"> &nbsp;&nbsp; 
				<input class="btn" type="button" value="Reset" onclick="javascript:toggleSRoutesForm();">
			</div>
		</div>
		
		<div><label>&nbsp;&nbsp;&nbsp;</label></div>
		<div><label>&nbsp;&nbsp;&nbsp;</label></div>
		
		{% if form.sroutes %}
		<div>
		<table width="95%" align="center" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<th class="heading">Network</th>
			<th class="heading">Gateway</th>
			<th class="heading">Subnet Mask</th>
			<th class="heading">Metric</th>
			<th class="heading">Interface</th>
			<th class="heading" width="10%">Action</th>
		</tr>
		{% for routes in form.sroutes %}
		<tr>
			<td class="element"><input type="hidden" id="srouteIP{{forloop.counter}}" name="srouteIP{{forloop.counter}}" value="{{ routes.0 }}">{{ routes.0 }}</td>
			<td class="element"><input type="hidden" id="srouteGateway{{forloop.counter}}" name="srouteGateway{{forloop.counter}}" value="{{ routes.1 }}">{{ routes.1 }}</td>
			<td class="element"><input type="hidden" id="srouteSubnet{{forloop.counter}}" name="srouteSubnet{{forloop.counter}}" value="{{ routes.2 }}">{{ routes.2 }}</td>
			<td class="element"><input type="hidden" id="srouteMetric{{forloop.counter}}" name="srouteMetric{{forloop.counter}}" value="{{ routes.3 }}">{{ routes.3 }}</td>
			<td class="element"><input type="hidden" id="srouteInterface{{forloop.counter}}" name="srouteInterface{{forloop.counter}}" value="{{ routes.4 }}">{{ routes.4 }}</td>
			<td width="10%">
				<label><img class="action" align="left" title="edit static route" onclick="javascript:showLoading('Loading..');get('editStaticRoute?ip={{ routes.0 }}\&gw={{ routes.1 }}\&nm={{ routes.2 }}\&metric={{ routes.3 }}\&iface={{ routes.4 }}','content-right');" src="/img/sroute_edit.png"/></label>
				<label><img class="action" align="right" title="delete static route" onclick="javascript:confirmSRouteDelete('{{ routes.0 }}','{{ routes.1 }}','{{ routes.2 }}','{{ routes.3 }}','{{ routes.4 }}');" src="/img/sroute_delete.png"/></label>
			</td>
		</tr>
		{% endfor %}
		</table>
		</div>
		{% endif %}
	</form>
	
	<div><label>&nbsp;&nbsp;&nbsp;</label></div>
	<div><label>&nbsp;&nbsp;&nbsp;</label></div>

	<form id="changeProxyForm" name="changeProxyForm" action="changeProxy" method="post" class="cssForm" onsubmit="return postForm(this,'content-right')">
		<div class="sectionHeading">Network Connection Settings</div>
		<div class="requiredFields">
			* Indicates mandatory fields.
		</div>
		{% if proxyErrors %}
		<div style="padding: 5px 0px;">
			<ul>
			    {% for error in proxyErrors %}
			    	<li class="errorMsg">{{ error }}</li>
			    {% endfor %}
			</ul>
		</div>
		{% endif %}
		<li class="info">{{proxyStatus}}</li>
		<div class="formDiv">
			<label class="formLabel">
				<input type="radio" name="connection" value="direct" {% if proxyForm.connection == 'direct' %}checked="checked" {% endif %} onclick="toggleProxy(this.value);"/>
				<span>&nbsp;&nbsp;&nbsp;</span>
			</label>
			Direct connection to the Internet
		</div>

		<div class="formDiv">
		 	<label class="formLabel">
				<input type="radio" name="connection" value="proxy" {% if proxyForm.connection == 'proxy' %}checked="checked" {% endif %} onclick="toggleProxy(this.value);"/>
				<span>&nbsp;&nbsp;&nbsp;</span> 
			</label>
			Manual proxy configuration
		</div>

		<div><label>&nbsp;&nbsp;&nbsp;</label></div>
		
		<div class="formDiv">
			<label class="formLabel">HTTP/HTTPS Proxy IP <span class="required">*</span> : </label>
			<input type="text" id="proxy_ip" name="proxy_ip" value="{% dict_lookup  proxyForm 'proxy_ip' %}">
		</div>
		
		<div class="formDiv">
			<label class="formLabel">Port <span class="required">*</span> : </label>
			<input type="text" id="proxy_port" name="proxy_port" value="{% dict_lookup  proxyForm 'proxy_port' %}">
		</div>

		<div class="formDiv">
			<label class="formLabel">User  : </label>
			<input type="text" id="proxy_usr" name="proxy_usr" value="{% dict_lookup  proxyForm 'proxy_usr' %}">
		</div>

		<div class="formDiv">
			<label class="formLabel">Password  : </label>
			<input type="text" id="proxy_pwd" name="proxy_pwd" value="{% dict_lookup  proxyForm 'proxy_pwd' %}">
		</div>
			
		<div class="formDiv">
			<label class="formLabel">&nbsp;</label>
			<div class="formBtnFormat">
				<input class="btn" type="submit" value="Save"> &nbsp;&nbsp; <input class="btn" type="button" value="Reset" onclick="javascript:toggleProxyForm();">
			</div>
		</div>
	</form>

	<div><label>&nbsp;&nbsp;&nbsp;</label></div>
	<div><label>&nbsp;&nbsp;&nbsp;</label></div>

	<div class="sectionHeading">Manage Network</div>
	<li class="info">{{restartStatus}}</li>
	<div class="content">
		<div class="infoDiv">
			<div class="left"> Restart Network Service : </div>
			<div class="right">
				<input class="btn" type="button" value="Restart Now" onclick="javascript:showLoading('Restarting please wait..');get('restartNetwork','content-right');""/>
			</div>
		</div>
	</div>		
	
<script  type="text/javascript" >

	$(document).ready(function(){
			toggleDHCP();
			toggleVlan();
			toggleLeftMenu('network');
			toggleProxy('{{proxyForm.connection}}');
		});	
	
	$('#dhcp').change(toggleDHCP);
	$('#nativeVlan').change(toggleVlan);
	
	function toggleDHCP() {
		elements = ['ipaddr','subnet','gateway','dns1','dns2'];
		checked = $('#dhcp').attr("checked")
		for (i=0;i<elements.length;i++) {
			if (checked) {
				$('#'+elements[i]).attr("disabled",'true');
			} else {
				$('#'+elements[i]).removeAttr("disabled");
			}
		}
	};
	
	function toggleVlan(){
		checked = $('#nativeVlan').attr("checked")
		if (checked) {
			$('#vlanid').removeAttr("disabled")
		} else {
			$('#vlanid').attr("disabled",'true')
		}
	};
	
	function toggleProxy(value) {
		if(value == 'proxy') {
			$('#proxy_ip').removeAttr("disabled");
			$('#proxy_port').removeAttr("disabled");
			$('#proxy_usr').removeAttr("disabled");
			$('#proxy_pwd').removeAttr("disabled");
		}else{
			$('#proxy_ip').attr("disabled",'true');
			$('#proxy_port').attr("disabled",'true');
			$('#proxy_usr').attr("disabled",'true');
			$('#proxy_pwd').attr("disabled",'true');
		}
	};
	
	function toggleProxyForm() {
		$1("changeProxyForm").reset();
		toggleProxy('{{proxyForm.connection}}');
		toggleLeftMenu('network');
	}
	
	function toggleVlanForm() {
		$1("saveVlanForm").reset();
		toggleLeftMenu('network');
	}
	
	function toggleAll() {
		$1("saveNetworkForm").reset();
		toggleDHCP();
		toggleVlan();
		toggleLeftMenu('network');
	};
	
	function confirmVlanDelete(vlan_iface){
		var msg,loadingMsg;
		msg = "Please confirm to delete this VLAN Configuration.";
		loadingMsg = "Deleting please wait..";
		var answer = confirm(msg)
		if(answer){
			showLoading(loadingMsg);
			get('deleteVlan?vlan=' + vlan_iface ,'content-right');
			return false;
		}
	}
	
	function confirmPhyIfaceDelete(iface){
		var msg,loadingMsg;
		msg = "Please confirm to delete this interface Configuration.";
		loadingMsg = "Deleting please wait..";
		var answer = confirm(msg)
		if(answer){
			showLoading(loadingMsg);
			get('deletePhyIface?iface=' + iface ,'content-right');
			return false;
		}
	}
	
	function toggleSRoutesForm() {
		$1("saveRouteForm").reset();
		toggleLeftMenu('network');
	}
	
	function confirmSRouteDelete(network, gateway, netmask, metric, iface){
		var msg,loadingMsg;
		msg = "Please confirm to delete this static route configuration.";
		loadingMsg = "Deleting please wait..";
		var answer = confirm(msg)
		if(answer){
			showLoading(loadingMsg);
			get('deleteStaticRoute?ip=' + network + '\&gw=' + gateway + '\&nm=' + netmask + '\&metric=' + metric + '\&iface=' + iface, 'content-right');
			return false;
		}
	}
</script>
